﻿/****************************************************************************
 * 模    块	：金额帮助类
 * 作    者	：Jasper
 * 编写时间	：2009-3-10
 * 实现功能	：将Decimal类型的数字转化为 中文大写金额。
 * 详细描述	：
 ****************************************************************************/

namespace Basic4Net.Utility.Tools
{
	using System;
	using System.Collections.Generic;
	using System.Text;

	public static class MoneyTools
	{
		private const string Digit = "零壹贰叁肆伍陆柒捌玖";		// 大写数字
		private const int ASCII = 48;							//'1'变化成1需要减少的int数字
		private static string[] Unit = { "分", "角", "元", "拾", "佰", "仟", "万", "亿" };

		/// <summary>
		/// 将Decimal类型的金额转化为大写金额
		/// </summary>
		/// <param name="Money">Decimal类型的金额。</param>
		/// <returns>String类型的大写字符串。</returns>
		public static string DecimalToString(Decimal Money)
		{
			string Result = String.Empty;
			StringBuilder SResult = new StringBuilder();
			string[] temp = Money.ToString().Split('.');
			if (temp.Length > 1 && temp[1].Length > 2)
				temp[1] = temp[1].Substring(0, 2);
			string IntString = temp[0];

			//处理小数位
			if (temp.Length > 1)
			{
				for (int i = temp[1].Length - 1; i > -1; i--)
				{
					if (i == 1)
						SResult.Append(Unit[0]);
					if (i == 0)
						SResult.Append(Unit[1]);
					SResult.Append(Digit[temp[1][i] - 48]);
				}
			}

			//添加元
			SResult.Append(Unit[2]);

			//处理整数位
			int j = 0;
			for (int i = IntString.Length - 1; i > -1; i--)
			{
				j++;
				int value = IntString[i] - 48;
				if (i > 0 && value == 0 && IntString[i - 1] - 48 == 0)
				{
					continue;
				}
				else
				{
					//if (j == 1)	//元
					//    SResult.Append(Unit[j + 1]);

					if (j == 2 && value != 0)	//拾
						SResult.Append(Unit[j + 1]);

					if (j == 3 && value != 0)	//佰
						SResult.Append(Unit[j + 1]);

					if (j == 4 && value != 0)	//仟
						SResult.Append(Unit[j + 1]);

					if (j == 5)	//万
						SResult.Append(Unit[j + 1]);

					if (j == 6 && value != 0)	//拾
						SResult.Append(Unit[j + 1 - 4]);

					if (j == 7 && value != 0)	//佰
						SResult.Append(Unit[j + 1 - 4]);

					if (j == 8 && value != 0)	//仟
						SResult.Append(Unit[j + 1 - 4]);

					if (j == 9)	//亿
						SResult.Append(Unit[j + 1 - 3]);

					if (j == 10 && value != 0)	//拾
						SResult.Append(Unit[j + 1 - 8]);

					if (j == 11 && value != 0)	//佰
						SResult.Append(Unit[j + 1 - 8]);

					if (j == 12 && value != 0)	//仟
						SResult.Append(Unit[j + 1 - 8]);

					if (j == 13)	//万
						SResult.Append(Unit[j + 1 - 8]);

					if (j == 14)	//亿
						SResult.Append(Unit[j + 1 - 8]);

					SResult.Append(Digit[value]);
				}
			}
			//需要翻转
			Result = Reverse(SResult.ToString());

			//去掉零等
			Result = Result.Replace("零亿", "亿零");
			Result = Result.Replace("零万", "万零");
			Result = Result.Replace("零角零分", "");
			Result = Result.Replace("零分", "");
			if (Result != "零元")
				Result = Result.Replace("零元", "元");
			Result = Result.TrimStart('元');
			if (Result.IndexOf('角') == -1 && Result.IndexOf('分') == -1)
				Result += "整";
			//Result = Result.Substring(0, Result.IndexOf('分') + 1);
			return Result;
		}

		private static string Reverse(string str)
		{
			char[] oldstr = str.ToCharArray();
			char[] newstr = new char[oldstr.Length];

			//for (int i = 0; i < newstr.Length; i++)
			//{
			//    newstr[i] = oldstr[oldstr.Length - 1 - i];
			//}
			System.Threading.Tasks.Parallel.For(0, newstr.Length, (i) =>
			{
				newstr[i] = oldstr[oldstr.Length - 1 - i];
			});
			return new String(newstr);
		}
	}
}
